﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;

namespace Skybrud.UmbExt.UniversalMediaPicker.Providers.Issuu
{
    public class IssuuConfigControl : AbstractConfigControl
    {
        private IssuuConfig config;
        private TextBox apiKeyTextBox = new TextBox();
        private TextBox apiSecretTextBox = new TextBox();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                config.IssuuApiKey = apiKeyTextBox.Text;
                config.IssuuApiSecret = apiSecretTextBox.Text;

                return config.SerializeToJson();
            }
        }

        public IssuuConfigControl(IssuuProvider provider, string options)
        {
            //this.provider = provider;
            this.config = options.DeserializeJsonTo<IssuuConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                apiKeyTextBox.Text = config.IssuuApiKey;
                apiSecretTextBox.Text = config.IssuuApiSecret;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            apiKeyTextBox.ID = "apiKeyTextBox";
            apiKeyTextBox.CssClass = "guiInputText guiInputStandardSize";

            apiSecretTextBox.ID = "apiSecretTextBox";
            apiSecretTextBox.CssClass = "guiInputText guiInputStandardSize";

            Controls.Add(apiKeyTextBox);
            Controls.Add(apiSecretTextBox);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("Api Key:", "Please enter a valid Issuu Api key", apiKeyTextBox);
            writer.AddFormRow("Api Secret:", "Please enter a valid Issuu Secret key", apiSecretTextBox);
        }
    }
}